//
//  CartButtonPriceBar.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/15.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CartButtonPriceBar.h"

#define margin 10
#define marginTop 8
#define CART_PRICEBAR_DEFAULTTEXT_SIZE [DimensUtils getScaleX:14]
#define CART_PRICEBAR_SPARETEXT_SIZE [DimensUtils getScaleX:11]

@interface CartButtonPriceBar()
{
    UILabel *_countDefaultLabel;
    UILabel *_countPriceLabel;
    UILabel *_countSpareLabel;
    UIButton *_toAccountButton;
}
@end

@implementation CartButtonPriceBar

- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cart_calc_price_bg"]]];
        //1.默认的总计文字
        NSString *defaultText = @"总计 :";
        _countDefaultLabel = [[UILabel alloc]init];
        [_countDefaultLabel setTextColor:JXColor(242, 242, 242)];
        [_countDefaultLabel setText:defaultText];
        [_countDefaultLabel setFont:[UIFont systemFontOfSize:CART_PRICEBAR_DEFAULTTEXT_SIZE]];
        CGSize defaultTextSize = [defaultText sizeWithFont:[UIFont systemFontOfSize:CART_PRICEBAR_DEFAULTTEXT_SIZE]];
        [_countDefaultLabel setFrame:CGRectMake(margin, marginTop, defaultTextSize.width, defaultTextSize.height)];
        [self addSubview:_countDefaultLabel];
        //2.当前总价
        NSString *defaultPriceText = @"0";
        _countPriceLabel = [[UILabel alloc]init];
        [_countPriceLabel setTextColor:JXColor(248, 51, 143)];
        [_countPriceLabel setText:defaultPriceText];
        [_countPriceLabel setFont:[UIFont systemFontOfSize:CART_PRICEBAR_DEFAULTTEXT_SIZE]];
        CGSize defaultPriceSize = [defaultPriceText sizeWithFont:[UIFont systemFontOfSize:CART_PRICEBAR_DEFAULTTEXT_SIZE]];
        [_countPriceLabel setFrame:CGRectMake(CGRectGetMaxX(_countDefaultLabel.frame)+margin,marginTop, defaultPriceSize.width, defaultPriceSize.height)];
        [self addSubview:_countPriceLabel];
        //3.节省的价格
        NSString *defaultSpareText = @"为您节省了￥0";
        _countSpareLabel = [[UILabel alloc]init];
        [_countSpareLabel setTextColor:JXColor(230, 230, 230)];
        [_countSpareLabel setText:defaultSpareText];
        [_countSpareLabel setFont:[UIFont systemFontOfSize:CART_PRICEBAR_SPARETEXT_SIZE]];
        CGSize defaultSpareSize = [defaultSpareText sizeWithFont:[UIFont systemFontOfSize:CART_PRICEBAR_SPARETEXT_SIZE]];
        [_countSpareLabel setFrame:CGRectMake(margin, CGRectGetMaxY(_countDefaultLabel.frame), defaultSpareSize.width, defaultSpareSize.height)];
        [self addSubview:_countSpareLabel];
        
        //4.去结算按钮
        _toAccountButton =[[UIButton alloc]init];
        [_toAccountButton setTitle:@"去结算" forState:UIControlStateNormal];
        [_toAccountButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_toAccountButton setContentMode:UIViewContentModeCenter];
        [_toAccountButton setBackgroundImage:[UIImage resizedImage:@"btn_login_bg"] forState:UIControlStateNormal];
        [_toAccountButton addTarget:self action:@selector(toAccountCenter) forControlEvents:UIControlEventTouchUpInside];
        [_toAccountButton setFrame:CGRectMake(JXScreenW-97-JXPublicInset,marginTop, 97.5, 32.5)];
        [self addSubview:_toAccountButton];
        [self setBounds:CGRectMake(0, 0, JXScreenW, 52.5)];
        
    }
    return self;
}
-(void)setPriceNum:(double)priceNum{
    NSString *price = [NSString stringWithFormat:@"%.1f",priceNum];
    CGSize priceSize = [price sizeWithFont:[UIFont systemFontOfSize:CART_PRICEBAR_DEFAULTTEXT_SIZE]];
    [_countPriceLabel setSize:priceSize];
    [_countPriceLabel setText:price];

}
-(void)setSpareNum:(double)spareNum{
    NSString *spare = [NSString stringWithFormat:@"为您节省了￥%.1f",spareNum];
    CGSize spareSize = [spare sizeWithFont:[UIFont systemFontOfSize:CART_PRICEBAR_SPARETEXT_SIZE]];
    [_countSpareLabel setSize:spareSize];
    [_countSpareLabel setText:spare];
}
//去结算
-(void)toAccountCenter
{
    if ([self.delegate respondsToSelector:@selector(toAccountWithPostParams)]) {
        [_delegate toAccountWithPostParams];
    }
}
@end
